const mysql = require('../../utils/db');
const BaseModel = require('../BaseModel');
const _table_name = "classify_tag";

exports.findAll = (success, fail, _all = false) => {
    let order = " ORDER BY id DESC";
    let sqlStr = `select * from ${_table_name}`;

    return mysql.row(sqlStr)
        .then(data => {
            success(data);
        })
        .catch(err => {
            fail(err);
        });
}

exports.updateClassifyTag = async (params, updatekey, success, fail) => {
    let sqlStr = BaseModel.update(_table_name, params, updatekey);

    return mysql.row(sqlStr)
        .then(data => {
            success(data);
        })
        .catch(err => {
            fail(err);
        });
}

exports.findClassifyTag = async (params) => {
    this.data = null;
    this.err = null;
    let sqlStr = BaseModel.find(_table_name, params);

    await mysql.row(sqlStr)
        .then(data => {
            this.data = data;
        })
        .catch(err => {
            this.err = err;
        });

    this.success = (fn) => {
        if (this.data)
            return fn(this.data);
    }
    this.fail = (fn) => {
        if (this.err)
            return fn(this.err);
    }
    return this;
}

exports.delClassifyTag = (params, success, fail) => {
    let sqlStr = BaseModel.delete(_table_name, params);
    return mysql.row(sqlStr)
        .then(data => {
            success(data);
        })
        .catch(err => {
            fail(err);
        });
}

exports.addClassifyTag = (params, success, fail) => {
    let sqlStr = BaseModel.add(_table_name, params);
    return mysql.row(sqlStr)
        .then(data => {
            success(data);
        })
        .catch(err => {
            fail(err);
        });
}